<!DOCTYPE html>
<html lang="en" class="js csstransforms3d">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="generator" content="Hugo 0.74.0-DEV" />
    <meta name="description" content="简单的个人网站">
<meta name="author" content="Ivan">

    <link rel="icon" href="/images/favicon.png" type="image/png">

    <title>Two Sum :: 云中子</title>

    
    <link href="/css/nucleus.css?1592985816" rel="stylesheet">
    <link href="/css/fontawesome-all.min.css?1592985816" rel="stylesheet">
    <link href="/css/hybrid.css?1592985816" rel="stylesheet">
    <link href="/css/featherlight.min.css?1592985816" rel="stylesheet">
    <link href="/css/perfect-scrollbar.min.css?1592985816" rel="stylesheet">
    <link href="/css/auto-complete.css?1592985816" rel="stylesheet">
    <link href="/css/atom-one-dark-reasonable.css?1592985816" rel="stylesheet">
    <link href="/css/theme.css?1592985816" rel="stylesheet">
    <link href="/css/hugo-theme.css?1592985816" rel="stylesheet">
    
    <link href="/css/theme-Red.css?1592985816" rel="stylesheet">
    
    

    <script src="/js/jquery-3.3.1.min.js?1592985816"></script>

    <style>
      :root #header + #content > #left > #rlblock_left{
          display:none !important;
      }
      
    </style>
    
  </head>
  <body class="" data-url="/%E5%B8%B8%E7%94%A8%E7%AE%97%E6%B3%95/letcode%E5%88%B7%E9%A2%98/twosum/">
    <nav id="sidebar" class="">



  <div id="header-wrapper">
    <div id="header">
      <a id="logo" href="https://ivanwangwhisper.gitee.io/">
  <svg id="grav-logo" style="width:100%; height:100%;" viewBox="0 0 504 140" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" style="fill-rule:evenodd;clip-rule:evenodd;stroke-linejoin:round;stroke-miterlimit:1.41421;">
    <path d="M235.832,71.564l-7.98,-0.001c-1.213,0.001 -2.197,0.987 -2.197,2.204l0,15.327l-0.158,0.132c-4.696,3.962 -10.634,6.14 -16.719,6.14c-14.356,0 -26.034,-11.68 -26.034,-26.037c0,-14.358 11.678,-26.035 26.034,-26.035c5.582,0 10.919,1.767 15.437,5.113c0.877,0.649 2.093,0.56 2.866,-0.211l5.69,-5.69c0.444,-0.442 0.675,-1.055 0.639,-1.681c-0.034,-0.627 -0.336,-1.206 -0.828,-1.597c-6.76,-5.363 -15.214,-8.314 -23.805,-8.314c-21.18,0 -38.414,17.233 -38.414,38.415c0,21.183 17.234,38.415 38.414,38.415c10.937,0 21.397,-4.705 28.698,-12.914c0.358,-0.403 0.556,-0.921 0.556,-1.46l0,-19.603c0,-1.217 -0.985,-2.203 -2.2,-2.203"
      style="fill:#000;fill-rule:nonzero;"></path>
    <path d="M502.794,34.445c-0.408,-0.616 -1.1,-0.989 -1.838,-0.989l-8.684,0c-0.879,0 -1.673,0.522 -2.022,1.329l-24.483,56.839l-24.92,-56.852c-0.352,-0.799 -1.142,-1.316 -2.012,-1.316l-8.713,0c-0.744,0 -1.44,0.373 -1.843,0.995c-0.408,0.623 -0.476,1.408 -0.174,2.09l30.186,68.858c0.352,0.799 1.143,1.317 2.017,1.317l10.992,0c0.879,0 1.673,-0.527 2.021,-1.329l29.655,-68.861c0.289,-0.68 0.222,-1.461 -0.182,-2.081"
      style="fill:#000;fill-rule:nonzero;"></path>
    <path d="M388.683,34.772c-0.353,-0.798 -1.142,-1.316 -2.017,-1.316l-10.988,0c-0.879,0 -1.673,0.522 -2.021,1.329l-29.655,68.861c-0.294,0.675 -0.226,1.46 0.182,2.077c0.407,0.619 1.096,0.993 1.838,0.993l8.684,0c0.879,0 1.673,-0.526 2.022,-1.329l24.478,-56.842l24.92,56.854c0.353,0.798 1.143,1.317 2.013,1.317l8.717,0c0.744,0 1.44,-0.374 1.843,-0.993c0.408,-0.624 0.471,-1.41 0.174,-2.094l-30.19,-68.857Z"
      style="fill:#000;fill-rule:nonzero;"></path>
    <path d="M309.196,81.525l0.476,-0.229c8.675,-4.191 14.279,-13.087 14.279,-22.667c0,-13.881 -11.295,-25.174 -25.176,-25.174l-31.863,0c-1.214,0 -2.199,0.988 -2.199,2.202l0,68.855c0,1.219 0.985,2.204 2.199,2.204l7.979,0c1.214,0 2.2,-0.985 2.2,-2.204l0,-58.679l21.684,0c7.059,0 12.799,5.739 12.799,12.796c0,5.885 -3.996,10.989 -9.728,12.408c-1.032,0.261 -2.064,0.393 -3.071,0.393l-7.977,0c-0.829,0 -1.585,0.467 -1.959,1.205c-0.378,0.74 -0.305,1.625 0.187,2.296l22.62,30.884c0.412,0.566 1.07,0.901 1.771,0.901l9.915,0c0.827,0 1.587,-0.467 1.96,-1.207c0.378,-0.742 0.302,-1.629 -0.186,-2.296l-15.91,-21.688Z"
      style="fill:#000;fill-rule:nonzero;"></path>
    <path d="M107.191,80.969c-7.255,-4.794 -11.4,-8.845 -15.011,-16.109c-2.47,4.977 -8.236,12.376 -17.962,18.198c-4.856,15.106 -27.954,44.015 -35.43,39.916c-2.213,-1.212 -2.633,-2.808 -2.133,-4.456c0.536,-4.129 9.078,-13.62 9.078,-13.62c0,0 0.18,1.992 2.913,6.187c-3.609,-11.205 5.965,-25.031 8.5,-29.738c3.985,-1.269 4.274,-6.387 4.274,-6.387c0.255,-7.909 -3.278,-13.635 -6.701,-17.059c2.459,3.002 3.255,7.539 3.372,11.694l0,0.023c0.012,0.469 0.012,0.93 0.011,1.39c-0.117,3.439 -1.157,8.19 -3.383,8.19l0.006,0.03c-2.289,-0.098 -5.115,0.391 -7.639,1.18l-5.582,1.334c0,0 2.977,-0.136 4.584,1.252c-1.79,2.915 -5.769,6.533 -10.206,8.588c-6.457,2.995 -8.312,-2.964 -5.034,-6.838c0.805,-0.946 1.618,-1.745 2.387,-2.399c-0.495,-0.513 -0.807,-1.198 -0.889,-2.068c-0.001,-0.005 -0.004,-0.009 -0.005,-0.013c-0.45,-1.977 -0.202,-4.543 2.596,-8.623c0.551,-0.863 1.214,-1.748 2.007,-2.647c0.025,-0.031 0.046,-0.059 0.072,-0.089c0.034,-0.042 0.072,-0.08 0.108,-0.121c0.02,-0.023 0.039,-0.045 0.059,-0.068c0.2,-0.228 0.413,-0.45 0.639,-0.663c3.334,-3.414 8.599,-6.966 16.897,-10.152c9.675,-14.223 13.219,-16.89 13.219,-16.89c1.071,-1.096 2.943,-2.458 3.632,-2.805c-5.053,-8.781 -6.074,-21.158 -4.75,-24.493c-0.107,0.18 -0.206,0.365 -0.287,0.556c0.49,-1.143 0.819,-1.509 1.328,-2.111c1.381,-1.632 6.058,-2.488 7.737,0.971c0.895,1.844 1.063,4.232 1.034,6.023c-3.704,-0.193 -7.063,4.036 -7.063,4.036c0,0 3.067,-1.448 6.879,-1.473c0,0 1.015,0.883 2.283,2.542c-1.712,3.213 -4.524,10.021 -2.488,17.168c0.338,1.408 0.849,2.619 1.483,3.648c0.024,0.045 0.044,0.089 0.069,0.135c0.051,0.066 0.096,0.122 0.144,0.183c3.368,5.072 9.542,5.665 9.542,5.665c-2.906,-1.45 -5.274,-3.76 -6.816,-6.56c-0.8,-1.498 -1.291,-2.762 -1.592,-3.761c-1.636,-6.313 0.771,-9.999 2.149,-12.471c3.17,-4.917 8.944,-7.893 15.151,-7.185c8.712,0.995 14.968,8.862 13.973,17.571c-0.608,5.321 -3.781,9.723 -8.142,12.117c1.049,2.839 -0.073,6.28 -0.073,6.28c2.642,3.323 2.758,5.238 2.667,7.017c-3.357,-0.565 -6.618,1.701 -6.618,1.701c0,0 6.476,-1.546 10.238,1.81c2.446,2.631 4.078,5.009 5.051,6.766c1.393,2.505 7.859,2.683 7.123,7.188c-0.737,4.499 -5.669,4.542 -13.401,-0.56M69.571,0c-38.424,0 -69.571,31.148 -69.571,69.567c0,38.422 31.147,69.573 69.571,69.573c38.42,0 69.568,-31.151 69.568,-69.573c0,-38.42 -31.148,-69.567 -69.568,-69.567"
      style="fill:#000;fill-rule:nonzero;"></path>
    <path d="M73.796,51.693c0.813,-0.814 0.813,-2.134 0,-2.947c-0.815,-0.814 -2.133,-0.814 -2.947,0c-0.815,0.813 -0.815,2.133 0,2.947c0.814,0.813 2.132,0.813 2.947,0" style="fill:#000;fill-rule:nonzero;"></path>
    <path d="M66.445,53.149c-0.814,0.813 -0.814,2.133 0,2.947c0.813,0.814 2.133,0.814 2.947,0c0.813,-0.814 0.813,-2.134 0,-2.947c-0.814,-0.813 -2.134,-0.813 -2.947,0" style="fill:#000;fill-rule:nonzero;"></path>
    <path d="M79.231,54.233c-1.274,-1.274 -3.339,-1.272 -4.611,0l-2.713,2.712c-1.274,1.275 -1.274,3.339 0,4.612l2.978,2.978c1.274,1.275 3.338,1.274 4.611,0l2.712,-2.712c1.274,-1.274 1.274,-3.339 0,-4.612l-2.977,-2.978Z" style="fill:#000;fill-rule:nonzero;"></path>
    <path d="M95.759,41.445c-2.151,-2.578 1.869,-7.257 4.391,-4.463c4.645,5.148 -2.237,7.041 -4.391,4.463M105.004,44.132c3.442,-6.553 -1.427,-10.381 -4.773,-13.523c-5.36,-5.039 -10.706,-7.217 -16.811,-0.241c-6.102,6.977 -2.226,15.068 3.356,19.061c5.584,3.994 14.782,1.255 18.228,-5.297"
      style="fill:#000;fill-rule:nonzero;"></path>
  </svg>
</a>

    </div>
    
        <div class="searchbox">
    <label for="search-by"><i class="fas fa-search"></i></label>
    <input data-search-input id="search-by" type="search" placeholder="Search...">
    <span data-search-clear=""><i class="fas fa-times"></i></span>
</div>

<script type="text/javascript" src="/js/lunr.min.js?1592985816"></script>
<script type="text/javascript" src="/js/auto-complete.js?1592985816"></script>
<script type="text/javascript">
    
        var baseurl = "https:\/\/ivanwangwhisper.gitee.io\/";
    
</script>
<script type="text/javascript" src="/js/search.js?1592985816"></script>

    
  </div>

    <div class="highlightable">
    <ul class="topics">

        
          
          




 
  
    
    <li data-nav-id="/ide%E4%BD%BF%E7%94%A8/" title="IDE使用" class="dd-item 
        
        
        
        ">
      <a href="/ide%E4%BD%BF%E7%94%A8/">
          <b>X. </b>IDE使用
          
      </a>
      
              
    </li>
  
 

          
          




 
  
    
    <li data-nav-id="/%E5%B8%B8%E7%94%A8%E7%AE%97%E6%B3%95/" title="常用算法" class="dd-item 
        parent
        
        
        ">
      <a href="/%E5%B8%B8%E7%94%A8%E7%AE%97%E6%B3%95/">
          <b>X. </b>常用算法
          
      </a>
      
      
        <ul>
          
          
            
          
          
          
        
          
            
            




 
  
    
    <li data-nav-id="/%E5%B8%B8%E7%94%A8%E7%AE%97%E6%B3%95/letcode%E5%88%B7%E9%A2%98/" title="LetCode刷题" class="dd-item 
        parent
        
        
        ">
      <a href="/%E5%B8%B8%E7%94%A8%E7%AE%97%E6%B3%95/letcode%E5%88%B7%E9%A2%98/">
          <b>X. </b>LetCode刷题
          
      </a>
      
      
        <ul>
          
          
          
          
        
          
            
            




 
  
    
      <li data-nav-id="/%E5%B8%B8%E7%94%A8%E7%AE%97%E6%B3%95/letcode%E5%88%B7%E9%A2%98/twosum/" title="Two Sum" class="dd-item active">
        <a href="/%E5%B8%B8%E7%94%A8%E7%AE%97%E6%B3%95/letcode%E5%88%B7%E9%A2%98/twosum/">
        Two Sum
        
        </a>
    </li>
     
  
 

            
          
        
        </ul>
              
    </li>
  
 

            
          
            
            




 
  
    
    <li data-nav-id="/%E5%B8%B8%E7%94%A8%E7%AE%97%E6%B3%95/%E5%B8%B8%E8%A7%81%E7%AE%97%E6%B3%95/" title="常见算法" class="dd-item 
        
        
        
        ">
      <a href="/%E5%B8%B8%E7%94%A8%E7%AE%97%E6%B3%95/%E5%B8%B8%E8%A7%81%E7%AE%97%E6%B3%95/">
          <b>X. </b>常见算法
          
      </a>
      
      
        <ul>
          
          
            
          
          
          
        
          
            
            




 
  
    
    <li data-nav-id="/%E5%B8%B8%E7%94%A8%E7%AE%97%E6%B3%95/%E5%B8%B8%E8%A7%81%E7%AE%97%E6%B3%95/%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95/" title="排序算法" class="dd-item 
        
        
        
        ">
      <a href="/%E5%B8%B8%E7%94%A8%E7%AE%97%E6%B3%95/%E5%B8%B8%E8%A7%81%E7%AE%97%E6%B3%95/%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95/">
          <b>X. </b>排序算法
          
      </a>
      
              
    </li>
  
 

            
          
        
        </ul>
              
    </li>
  
 

            
          
        
        </ul>
              
    </li>
  
 

          
          




 
  
    
    <li data-nav-id="/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/" title="操作系统" class="dd-item 
        
        
        
        ">
      <a href="/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/">
          <b>X. </b>操作系统
          
      </a>
      
              
    </li>
  
 

          
          




 
  
    
    <li data-nav-id="/%E6%B5%8B%E8%AF%95%E5%B7%A5%E5%85%B7/" title="测试工具" class="dd-item 
        
        
        
        ">
      <a href="/%E6%B5%8B%E8%AF%95%E5%B7%A5%E5%85%B7/">
          <b>X. </b>测试工具
          
      </a>
      
              
    </li>
  
 

          
          




 
  
    
    <li data-nav-id="/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/" title="编程语言" class="dd-item 
        
        
        
        ">
      <a href="/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/">
          <b>X. </b>编程语言
          
      </a>
      
      
        <ul>
          
          
            
          
          
          
        
          
            
            




 
  
    
    <li data-nav-id="/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/csharp/" title="CSharp" class="dd-item 
        
        
        
        ">
      <a href="/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/csharp/">
          <b>X. </b>CSharp
          
      </a>
      
      
        <ul>
          
          
            
          
          
          
        
          
            
            




 
  
    
    <li data-nav-id="/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/csharp/%E6%A1%86%E6%9E%B6/" title="框架" class="dd-item 
        
        
        
        ">
      <a href="/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/csharp/%E6%A1%86%E6%9E%B6/">
          <b>X. </b>框架
          
      </a>
      
      
        <ul>
          
          
          
          
        
          
            
            




 
  
    
      <li data-nav-id="/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/csharp/%E6%A1%86%E6%9E%B6/autofac/" title="Autofac" class="dd-item ">
        <a href="/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/csharp/%E6%A1%86%E6%9E%B6/autofac/">
        Autofac
        
        </a>
    </li>
     
  
 

            
          
            
            




 
  
    
      <li data-nav-id="/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/csharp/%E6%A1%86%E6%9E%B6/automapper/" title="AutoMapper" class="dd-item ">
        <a href="/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/csharp/%E6%A1%86%E6%9E%B6/automapper/">
        AutoMapper
        
        </a>
    </li>
     
  
 

            
          
        
        </ul>
              
    </li>
  
 

            
          
            
            




 
  
    
    <li data-nav-id="/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/csharp/%E7%96%91%E9%9A%BE%E6%9D%82%E7%97%87/" title="疑难杂症" class="dd-item 
        
        
        
        ">
      <a href="/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/csharp/%E7%96%91%E9%9A%BE%E6%9D%82%E7%97%87/">
          <b>X. </b>疑难杂症
          
      </a>
      
      
        <ul>
          
          
          
          
        
          
            
            




 
  
    
      <li data-nav-id="/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/csharp/%E7%96%91%E9%9A%BE%E6%9D%82%E7%97%87/usewriteablebitmap/" title="更新图片内存暴涨" class="dd-item ">
        <a href="/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/csharp/%E7%96%91%E9%9A%BE%E6%9D%82%E7%97%87/usewriteablebitmap/">
        更新图片内存暴涨
        
        </a>
    </li>
     
  
 

            
          
        
        </ul>
              
    </li>
  
 

            
          
        
        </ul>
              
    </li>
  
 

            
          
            
            




 
  
    
    <li data-nav-id="/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/dart/" title="Dart" class="dd-item 
        
        
        
        ">
      <a href="/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/dart/">
          <b>X. </b>Dart
          
      </a>
      
              
    </li>
  
 

            
          
            
            




 
  
    
    <li data-nav-id="/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/golang/" title="Golang" class="dd-item 
        
        
        
        ">
      <a href="/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/golang/">
          <b>X. </b>Golang
          
      </a>
      
              
    </li>
  
 

            
          
            
            




 
  
    
    <li data-nav-id="/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/java/" title="Java" class="dd-item 
        
        
        
        ">
      <a href="/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/java/">
          <b>X. </b>Java
          
      </a>
      
              
    </li>
  
 

            
          
            
            




 
  
    
    <li data-nav-id="/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/python/" title="Python" class="dd-item 
        
        
        
        ">
      <a href="/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/python/">
          <b>X. </b>Python
          
      </a>
      
              
    </li>
  
 

            
          
        
        </ul>
              
    </li>
  
 

          
          




 
  
    
    <li data-nav-id="/%E8%BF%90%E7%BB%B4%E9%83%A8%E7%BD%B2/" title="运维部署" class="dd-item 
        
        
        
        ">
      <a href="/%E8%BF%90%E7%BB%B4%E9%83%A8%E7%BD%B2/">
          <b>X. </b>运维部署
          
      </a>
      
              
    </li>
  
 

          
          




 
  
    
    <li data-nav-id="/%E9%A1%B9%E7%9B%AE%E7%BB%84%E4%BB%B6/" title="项目组件" class="dd-item 
        
        
        
        ">
      <a href="/%E9%A1%B9%E7%9B%AE%E7%BB%84%E4%BB%B6/">
          <b>X. </b>项目组件
          
      </a>
      
              
    </li>
  
 

          
         
    </ul>

    
    
      <section id="shortcuts">
        <h3>More</h3>
        <ul>
          
              <li> 
                  <a class="padding" href="https://themes.gohugo.io//theme/hugo-theme-learn/en"><i class='fa fa-book' aria-hidden='true'></i> Learn Book</a>
              </li>
          
              <li> 
                  <a class="padding" href="https://ivanwangwhisper.gitee.io/"><i class='fa fa-book' aria-hidden='true'></i> My Doc</a>
              </li>
          
              <li> 
                  <a class="padding" href="https://github.com/IvanWhisper"><i class='fab fa-github'></i> My Github</a>
              </li>
          
              <li> 
                  <a class="padding" href="https://gitee.com/ivanwangwhisper"><i class='fab fa-github'></i> MyGitee</a>
              </li>
          
              <li> 
                  <a class="padding" href="https://ivanwangwhisper.gitee.io/tags"><i class='fas fa-tags'></i> Tags</a>
              </li>
          
        </ul>
      </section>
    

    
    <section id="footer">
      <p>Built with <a href="https://github.com/IvanWhisper"><i class="fas fa-heart"></i></a> from <a href="https://getgrav.org">Grav</a> and <a href="https://gohugo.io/">Hugo</a></p>

    </section>
  </div>
</nav>




        <section id="body">
        <div id="overlay"></div>
        <div class="padding highlightable">
              
              <div>
                <div id="top-bar">
                
                
                <div id="breadcrumbs" itemscope="" itemtype="http://data-vocabulary.org/Breadcrumb">
                    <span id="sidebar-toggle-span">
                        <a href="#" id="sidebar-toggle" data-sidebar-toggle="">
                          <i class="fas fa-bars"></i>
                        </a>
                    </span>
                  
                  <span id="toc-menu"><i class="fas fa-list-alt"></i></span>
                  
                  <span class="links">
                 
                 
                    
          
          
            
            
          
          
            
            
          
          
            
            
          
          
            <a href='/'>欢迎光临</a> > <a href='/%E5%B8%B8%E7%94%A8%E7%AE%97%E6%B3%95/'>常用算法</a> > <a href='/%E5%B8%B8%E7%94%A8%E7%AE%97%E6%B3%95/letcode%E5%88%B7%E9%A2%98/'>LetCode刷题</a> > Two Sum
          
        
          
        
          
        
          
        
                 
                  </span>
                </div>
                
                    <div class="progress">
    <div class="wrapper">
<nav id="TableOfContents">
  <ul>
    <li>
      <ul>
        <li><a href="#题目">题目</a></li>
        <li><a href="#思路">思路</a></li>
        <li><a href="#示例代码">示例代码</a></li>
      </ul>
    </li>
  </ul>
</nav>
    </div>
</div>

                
              </div>
            </div>
            
        <div id="head-tags">
        
        </div>
        
        <div id="body-inner">
          
            <h1>
              
              Two Sum
            </h1>
          

        


<h3 id="题目">题目</h3>
<p>Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
给定一个整数数组 nums 和一个目标值 target，请你在该数组中找出和为目标值的那 两个 整数，并返回他们的数组下标。
你可以假设每种输入只会对应一个答案。但是，你不能重复利用这个数组中同样的元素。
示例:
给定 nums = [2, 7, 11, 15], target = 9
因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]</p>
<h3 id="思路">思路</h3>
<ul>
<li>不能重复利用这个数组中同样的元素</li>
<li>输入只会对应一个答案</li>
<li>返回他们的数组下标</li>
</ul>
<h3 id="示例代码">示例代码</h3>
<h4 id="c">C#</h4>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-C#" data-lang="C#"><span style="color:#66d9ef">using</span> System;
<span style="color:#66d9ef">using</span> System.Collections.Generic;
<span style="color:#66d9ef">using</span> System.Linq;

<span style="color:#66d9ef">namespace</span> Day0001.TwoSum
{
    <span style="color:#66d9ef">class</span> <span style="color:#a6e22e">Program</span>
    {
        <span style="color:#66d9ef">static</span> <span style="color:#66d9ef">void</span> Main(<span style="color:#66d9ef">string</span>[] args)
        {
            Console.WriteLine(<span style="color:#e6db74">&#34;输入：&#34;</span>);
            <span style="color:#66d9ef">var</span> nums = <span style="color:#66d9ef">new</span> <span style="color:#66d9ef">int</span>[] { <span style="color:#ae81ff">2</span>, <span style="color:#ae81ff">7</span>, <span style="color:#ae81ff">11</span>, <span style="color:#ae81ff">15</span> };
            <span style="color:#66d9ef">var</span> target = <span style="color:#ae81ff">9</span>;
            Print(nums);
            Console.WriteLine(<span style="color:#e6db74">$&#34;目标值:{target}&#34;</span>);
            
            <span style="color:#66d9ef">var</span> res1 =<span style="color:#66d9ef">new</span> Solution().TwoSum1(nums, target);
            <span style="color:#66d9ef">var</span> res2=<span style="color:#66d9ef">new</span> Solution().TwoSum2(nums, target);
            <span style="color:#66d9ef">var</span> res3=<span style="color:#66d9ef">new</span> Solution().TwoSum3(nums, target);

            Console.WriteLine(<span style="color:#e6db74">&#34;输出：&#34;</span>);
            Console.WriteLine(<span style="color:#e6db74">$&#34;暴力法：&#34;</span>);
            Print(res1);
            Console.WriteLine(<span style="color:#e6db74">$&#34;两遍哈希表：&#34;</span>);
            Print(res2);
            Console.WriteLine(<span style="color:#e6db74">$&#34;一遍哈希表：&#34;</span>);
            Print(res3);
            Console.ReadLine();
        }
        <span style="color:#66d9ef">public</span> <span style="color:#66d9ef">static</span> <span style="color:#66d9ef">void</span> Print(<span style="color:#66d9ef">int</span>[] nums)
        {
            nums.ToList().ForEach(o =&gt; Console.Write(o+<span style="color:#e6db74">&#34; &#34;</span>));
            Console.WriteLine();
        }
    }
    <span style="color:#66d9ef">public</span> <span style="color:#66d9ef">class</span> <span style="color:#a6e22e">Solution</span>
    {
        <span style="color:#75715e">/// &lt;summary&gt;
</span><span style="color:#75715e"></span>        <span style="color:#75715e">/// 暴力法(循环2次)
</span><span style="color:#75715e"></span>        <span style="color:#75715e">/// &lt;/summary&gt;
</span><span style="color:#75715e"></span>        <span style="color:#75715e">/// &lt;param name=&#34;nums&#34;&gt;&lt;/param&gt;
</span><span style="color:#75715e"></span>        <span style="color:#75715e">/// &lt;param name=&#34;target&#34;&gt;&lt;/param&gt;
</span><span style="color:#75715e"></span>        <span style="color:#75715e">/// &lt;returns&gt;&lt;/returns&gt;
</span><span style="color:#75715e"></span>        <span style="color:#66d9ef">public</span> <span style="color:#66d9ef">int</span>[] TwoSum1(<span style="color:#66d9ef">int</span>[] nums, <span style="color:#66d9ef">int</span> target)
        {
            <span style="color:#66d9ef">for</span>(<span style="color:#66d9ef">int</span> i = <span style="color:#ae81ff">0</span>; i &lt; nums.Length; i++)
            {
                <span style="color:#66d9ef">for</span>(<span style="color:#66d9ef">int</span> j = i+<span style="color:#ae81ff">1</span>; j &lt; nums.Length;j++)
                {
                    <span style="color:#66d9ef">if</span> (nums[i] + nums[j] == target)
                        <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">new</span> <span style="color:#66d9ef">int</span>[] { i, j };
                }
            }
            <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">null</span>;
        }
        <span style="color:#75715e">/// &lt;summary&gt;
</span><span style="color:#75715e"></span>        <span style="color:#75715e">/// 两遍哈希表(分开循环2次)
</span><span style="color:#75715e"></span>        <span style="color:#75715e">/// &lt;/summary&gt;
</span><span style="color:#75715e"></span>        <span style="color:#75715e">/// &lt;param name=&#34;nums&#34;&gt;&lt;/param&gt;
</span><span style="color:#75715e"></span>        <span style="color:#75715e">/// &lt;param name=&#34;target&#34;&gt;&lt;/param&gt;
</span><span style="color:#75715e"></span>        <span style="color:#75715e">/// &lt;returns&gt;&lt;/returns&gt;
</span><span style="color:#75715e"></span>        <span style="color:#66d9ef">public</span> <span style="color:#66d9ef">int</span>[] TwoSum2(<span style="color:#66d9ef">int</span>[] nums, <span style="color:#66d9ef">int</span> target)
        {
            <span style="color:#66d9ef">var</span> kvs = <span style="color:#66d9ef">new</span> Dictionary&lt;<span style="color:#66d9ef">int</span>, <span style="color:#66d9ef">int</span>&gt;();
            <span style="color:#66d9ef">for</span>(<span style="color:#66d9ef">int</span> i=<span style="color:#ae81ff">0</span>; i &lt; nums.Length; i++)
            {
                <span style="color:#75715e">//需要对重复值进行判断；因为结果的唯一，所以若有重复值
</span><span style="color:#75715e"></span>                <span style="color:#66d9ef">if</span> (kvs.ContainsKey(nums[i]))
                {
                    <span style="color:#75715e">//答案中包含了重复值的话,说明必有 重复值*2 == target
</span><span style="color:#75715e"></span>                    <span style="color:#66d9ef">if</span> (nums[i] * <span style="color:#ae81ff">2</span> == target)
                        <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">new</span> <span style="color:#66d9ef">int</span>[] { i, kvs[nums[i]] };
                    <span style="color:#75715e">//否则直接忽略重复值即可
</span><span style="color:#75715e"></span>                }
                <span style="color:#66d9ef">else</span>
                {
                    kvs.Add(nums[i], i);
                }
            }
            <span style="color:#66d9ef">for</span>(<span style="color:#66d9ef">int</span> i = <span style="color:#ae81ff">0</span>; i &lt; nums.Length; i++)
            {
                <span style="color:#66d9ef">int</span> t = target - nums[i];
                <span style="color:#75715e">//直接忽略重复值即可
</span><span style="color:#75715e"></span>                <span style="color:#66d9ef">if</span> (kvs.ContainsKey(t) &amp;&amp; kvs[t] != i)
                {
                    <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">new</span> <span style="color:#66d9ef">int</span>[] { i, kvs[t] };
                }
            }
            <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">new</span> <span style="color:#66d9ef">int</span>[] { <span style="color:#ae81ff">0</span>, <span style="color:#ae81ff">0</span> };
        }
        <span style="color:#75715e">/// &lt;summary&gt;
</span><span style="color:#75715e"></span>        <span style="color:#75715e">/// 一边哈希表（一次循环）
</span><span style="color:#75715e"></span>        <span style="color:#75715e">/// &lt;/summary&gt;
</span><span style="color:#75715e"></span>        <span style="color:#75715e">/// &lt;param name=&#34;nums&#34;&gt;&lt;/param&gt;
</span><span style="color:#75715e"></span>        <span style="color:#75715e">/// &lt;param name=&#34;target&#34;&gt;&lt;/param&gt;
</span><span style="color:#75715e"></span>        <span style="color:#75715e">/// &lt;returns&gt;&lt;/returns&gt;
</span><span style="color:#75715e"></span>        <span style="color:#66d9ef">public</span> <span style="color:#66d9ef">int</span>[] TwoSum3(<span style="color:#66d9ef">int</span>[] nums, <span style="color:#66d9ef">int</span> target)
        {
            <span style="color:#66d9ef">var</span> kvs = <span style="color:#66d9ef">new</span> Dictionary&lt;<span style="color:#66d9ef">int</span>, <span style="color:#66d9ef">int</span>&gt;();
            <span style="color:#66d9ef">for</span> (<span style="color:#66d9ef">int</span> i = <span style="color:#ae81ff">0</span>; i &lt; nums.Length; i++)
            {
                <span style="color:#66d9ef">int</span> t = target - nums[i];
                <span style="color:#75715e">//直接忽略重复值即可
</span><span style="color:#75715e"></span>                <span style="color:#66d9ef">if</span> (kvs.ContainsKey(t) &amp;&amp; kvs[t] != i)
                {
                    <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">new</span> <span style="color:#66d9ef">int</span>[] { i, kvs[t] };
                }
                <span style="color:#75715e">//需要对重复值进行判断,若结果包含了重复值，则已经被上面给return了；所以此处对于重复值直接忽略
</span><span style="color:#75715e"></span>                <span style="color:#66d9ef">if</span> (!kvs.ContainsKey(nums[i]))
                {
                    kvs.Add(nums[i], i);
                }
            }
            <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">new</span> <span style="color:#66d9ef">int</span>[] { <span style="color:#ae81ff">0</span>, <span style="color:#ae81ff">0</span> };
        }
    }
}
</code></pre></div><h4 id="go">Go</h4>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-Go" data-lang="Go"><span style="color:#f92672">package</span> <span style="color:#a6e22e">main</span>

<span style="color:#f92672">import</span> <span style="color:#e6db74">&#34;fmt&#34;</span>

<span style="color:#66d9ef">func</span> <span style="color:#a6e22e">main</span>() {
	<span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#e6db74">&#34;输入&#34;</span>)
	<span style="color:#a6e22e">nums</span> <span style="color:#f92672">:=</span> []<span style="color:#66d9ef">int</span>{<span style="color:#ae81ff">2</span>, <span style="color:#ae81ff">7</span>, <span style="color:#ae81ff">11</span>, <span style="color:#ae81ff">15</span>}
	<span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#a6e22e">nums</span>)
	<span style="color:#a6e22e">target</span> <span style="color:#f92672">:=</span> <span style="color:#ae81ff">9</span>
	<span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#e6db74">&#34;目标值：&#34;</span>)
	<span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#a6e22e">target</span>)
	<span style="color:#a6e22e">res1</span><span style="color:#f92672">:=</span><span style="color:#a6e22e">twoSum1</span>(<span style="color:#a6e22e">nums</span>,<span style="color:#a6e22e">target</span>)
	<span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#e6db74">&#34;暴力法返回：&#34;</span>)
	<span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#a6e22e">res1</span>)
	<span style="color:#a6e22e">res2</span><span style="color:#f92672">:=</span><span style="color:#a6e22e">twoSum2</span>(<span style="color:#a6e22e">nums</span>,<span style="color:#a6e22e">target</span>)
	<span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#e6db74">&#34;哈希法返回：&#34;</span>)
	<span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#a6e22e">res2</span>)
}
<span style="color:#75715e">//暴力法
</span><span style="color:#75715e"></span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">twoSum1</span>(<span style="color:#a6e22e">nums</span> []<span style="color:#66d9ef">int</span>,<span style="color:#a6e22e">target</span> <span style="color:#66d9ef">int</span>) []<span style="color:#66d9ef">int</span> {
	<span style="color:#66d9ef">for</span> <span style="color:#a6e22e">i</span>,<span style="color:#a6e22e">vi</span><span style="color:#f92672">:=</span><span style="color:#66d9ef">range</span> <span style="color:#a6e22e">nums</span>{
		<span style="color:#66d9ef">for</span> <span style="color:#a6e22e">j</span>,<span style="color:#a6e22e">vj</span><span style="color:#f92672">:=</span><span style="color:#66d9ef">range</span> <span style="color:#a6e22e">nums</span>[(<span style="color:#a6e22e">i</span><span style="color:#f92672">+</span><span style="color:#ae81ff">1</span>):]{
			<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">vi</span><span style="color:#f92672">+</span><span style="color:#a6e22e">vj</span><span style="color:#f92672">==</span><span style="color:#a6e22e">target</span>{
				<span style="color:#66d9ef">return</span> []<span style="color:#66d9ef">int</span>{<span style="color:#a6e22e">i</span>,<span style="color:#a6e22e">j</span><span style="color:#f92672">+</span><span style="color:#a6e22e">i</span><span style="color:#f92672">+</span><span style="color:#ae81ff">1</span>}
			}
		}
	}
	<span style="color:#66d9ef">return</span> <span style="color:#66d9ef">nil</span>
}
<span style="color:#75715e">//哈希
</span><span style="color:#75715e"></span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">twoSum2</span>(<span style="color:#a6e22e">nums</span> []<span style="color:#66d9ef">int</span>,<span style="color:#a6e22e">target</span> <span style="color:#66d9ef">int</span>) []<span style="color:#66d9ef">int</span> {
	<span style="color:#a6e22e">maps</span><span style="color:#f92672">:=</span>make(<span style="color:#66d9ef">map</span>[<span style="color:#66d9ef">int</span>]<span style="color:#66d9ef">int</span>,len(<span style="color:#a6e22e">nums</span>)<span style="color:#f92672">/</span><span style="color:#ae81ff">2</span>)
	<span style="color:#66d9ef">for</span> <span style="color:#a6e22e">k</span>,<span style="color:#a6e22e">v</span><span style="color:#f92672">:=</span><span style="color:#66d9ef">range</span> <span style="color:#a6e22e">nums</span>{
		<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">num</span>,<span style="color:#a6e22e">ok</span><span style="color:#f92672">:=</span><span style="color:#a6e22e">maps</span>[<span style="color:#a6e22e">target</span><span style="color:#f92672">-</span><span style="color:#a6e22e">v</span>];<span style="color:#a6e22e">ok</span>{
			<span style="color:#66d9ef">return</span> []<span style="color:#66d9ef">int</span>{<span style="color:#a6e22e">num</span>,<span style="color:#a6e22e">k</span>}
		}
		<span style="color:#a6e22e">maps</span>[<span style="color:#a6e22e">v</span>]=<span style="color:#a6e22e">k</span>
	}
	<span style="color:#66d9ef">return</span> <span style="color:#66d9ef">nil</span>
}
</code></pre></div>

<footer class="footline">
	
</footer>

        
        </div> 
        

      </div>

    <div id="navigation">
        
        
        
        
            
            
                
                    
                    
                
                

                    
                    
                        
                    
                    

                    
                        
            
            
                
                    
                        
                        
                    
                
                

                    
                    
                    

                    
            
        
                    
                        
            
            
                
                    
                    
                
                

                    
                    
                        
                    
                    

                    
                        
            
            
                
                    
                    
                
                

                    
                    
                    

                    
                        
            
            
                
                    
                    
                
                

                    
                    
                    

                    
            
        
                    
            
        
                    
                        
            
            
                
                    
                        
                        
                    
                
                

                    
                    
                        
                    
                    

                    
                        
            
            
                
                    
                
                

                    
                    
                    

                    
            
        
                    
            
        
                    
            
        
                    
                        
            
            
                
                    
                
                

                    
                    
                    

                    
            
        
                    
                        
            
            
                
                    
                
                

                    
                    
                    

                    
            
        
                    
                        
            
            
                
                    
                
                

                    
                    
                        
                    
                    

                    
                        
            
            
                
                    
                
                

                    
                    
                        
                    
                    

                    
                        
            
            
                
                    
                
                

                    
                    
                    

                    
                        
            
            
                
                    
                
                

                    
                    
                    

                    
            
        
                    
                        
            
            
                
                    
                
                

                    
                    
                    

                    
            
        
                    
            
        
                    
                        
            
            
                
                    
                
                

                    
                    
                    

                    
                        
            
            
                
                    
                
                

                    
                    
                    

                    
            
        
                    
            
        
                    
            
        
                    
                        
            
            
                
                    
                
                

                    
                    
                    

                    
            
        
                    
                        
            
            
                
                    
                
                

                    
                    
                    

                    
            
        
                    
                        
            
            
                
                    
                
                

                    
                    
                    

                    
            
        
                    
                        
            
            
                
                    
                
                

                    
                    
                    

                    
            
        
                    
            
        
                    
                        
            
            
                
                    
                
                

                    
                    
                    

                    
            
        
                    
                        
            
            
                
                    
                
                

                    
                    
                    

                    
            
        
                    
            
        
        
        


	 
	 
		
			<a class="nav nav-prev" href="/%E5%B8%B8%E7%94%A8%E7%AE%97%E6%B3%95/letcode%E5%88%B7%E9%A2%98/" title="LetCode刷题"> <i class="fa fa-chevron-left"></i></a>
		
		
			<a class="nav nav-next" href="/%E5%B8%B8%E7%94%A8%E7%AE%97%E6%B3%95/%E5%B8%B8%E8%A7%81%E7%AE%97%E6%B3%95/" title="常见算法" style="margin-right: 0px;"><i class="fa fa-chevron-right"></i></a>
		
	
    </div>

    </section>
    
    <div style="left: -1000px; overflow: scroll; position: absolute; top: -1000px; border: none; box-sizing: content-box; height: 200px; margin: 0px; padding: 0px; width: 200px;">
      <div style="border: none; box-sizing: content-box; height: 200px; margin: 0px; padding: 0px; width: 200px;"></div>
    </div>
    <script src="/js/clipboard.min.js?1592985816"></script>
    <script src="/js/perfect-scrollbar.min.js?1592985816"></script>
    <script src="/js/perfect-scrollbar.jquery.min.js?1592985816"></script>
    <script src="/js/jquery.sticky.js?1592985816"></script>
    <script src="/js/featherlight.min.js?1592985816"></script>
    <script src="/js/highlight.pack.js?1592985816"></script>
    <script>hljs.initHighlightingOnLoad();</script>
    <script src="/js/modernizr.custom-3.6.0.js?1592985816"></script>
    <script src="/js/learn.js?1592985816"></script>
    <script src="/js/hugo-learn.js?1592985816"></script>

    <link href="/mermaid/mermaid.css?1592985816" rel="stylesheet" />
    <script src="/mermaid/mermaid.js?1592985816"></script>
    <script>
        mermaid.initialize({ startOnLoad: true });
    </script>
    

  </body>
</html>

